package com.baiyi.muyi.wxapi;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.widget.Toast;

import com.baiyinet.BuildConfig;
import com.baiyinet.biz.PageStatus;
import com.baiyinet.util.IntentUtils;
import com.baiyinet.pay.PayUtils;
import com.tencent.mm.opensdk.constants.ConstantsAPI;
import com.tencent.mm.opensdk.modelbase.BaseReq;
import com.tencent.mm.opensdk.modelbase.BaseResp;
import com.tencent.mm.opensdk.openapi.IWXAPI;
import com.tencent.mm.opensdk.openapi.IWXAPIEventHandler;
import com.tencent.mm.opensdk.openapi.WXAPIFactory;

public class WXPayEntryActivity extends Activity implements IWXAPIEventHandler {
	
    private IWXAPI api;
	
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

    	api = WXAPIFactory.createWXAPI(this, BuildConfig.WECHAT_APP_ID);
        api.handleIntent(getIntent(), this);
    }

	@Override
	protected void onNewIntent(Intent intent) {
		super.onNewIntent(intent);
		setIntent(intent);
        api.handleIntent(intent, this);
	}

	@Override
	public void onReq(BaseReq req) {
	}

	@Override
	public void onResp(BaseResp resp) {
		if (resp.getType() == ConstantsAPI.COMMAND_PAY_BY_WX) {
//			Toast.makeText(this, resp.errCode + "", Toast.LENGTH_SHORT).show();
			if(0 == resp.errCode) {
				// 跳转支付结果页面
				String url = PayUtils.PAY_RESULT_URL.startsWith("http") ? PayUtils.PAY_RESULT_URL
						: "http://".concat(PayUtils.PAY_RESULT_URL);

				PageStatus pageStatus = new PageStatus(url, true, true, PayUtils.TOOLBAR_BG_COLOR);
				IntentUtils.toNewGeneralActivity(this, pageStatus);

				PayUtils.PAY_RESULT_URL = "";
			}else {
				Toast.makeText(WXPayEntryActivity.this, "支付失败", Toast.LENGTH_SHORT).show();
			}
			finish();
		}
	}
}